package com.ruoyi.web.controller.system;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Purchase;
import com.ruoyi.system.service.IPurchaseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * Purchase 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-07-06
 */
@Controller
@RequestMapping("/system/purchase")
public class PurchaseController extends BaseController
{
    private String prefix = "system/purchase";
	
	@Autowired
	private IPurchaseService purchaseService;
	
	@RequiresPermissions("system:purchase:view")
	@GetMapping()
	public String purchase()
	{
	    return prefix + "/purchase";
	}
	
	/**
	 * 查询Purchase列表
	 */
	@RequiresPermissions("system:purchase:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Purchase purchase)
	{
		startPage();
        List<Purchase> list = purchaseService.selectPurchaseList(purchase);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出Purchase列表
	 */
	@RequiresPermissions("system:purchase:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Purchase purchase)
    {
    	List<Purchase> list = purchaseService.selectPurchaseList(purchase);
        ExcelUtil<Purchase> util = new ExcelUtil<Purchase>(Purchase.class);
        return util.exportExcel(list, "purchase");
    }
	
	/**
	 * 新增Purchase
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存Purchase
	 */
	@RequiresPermissions("system:purchase:add")
	@Log(title = "Purchase", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Purchase purchase)
	{		
		return toAjax(purchaseService.insertPurchase(purchase));
	}

	/**
	 * 修改Purchase
	 */
	@GetMapping("/edit/{purchaseId}")
	public String edit(@PathVariable("purchaseId") Integer purchaseId, ModelMap mmap)
	{
		Purchase purchase = purchaseService.selectPurchaseById(purchaseId);
		mmap.put("purchase", purchase);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存Purchase
	 */
	@RequiresPermissions("system:purchase:edit")
	@Log(title = "Purchase", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Purchase purchase)
	{		
		return toAjax(purchaseService.updatePurchase(purchase));
	}
	
	/**
	 * 删除Purchase
	 */
	@RequiresPermissions("system:purchase:remove")
	@Log(title = "Purchase", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(purchaseService.deletePurchaseByIds(ids));
	}
	
}
